package com.cloud.model.system;


import com.cloud.base.BaseEntity;
import com.cloud.model.Class;
import com.cloud.model.Template;
import com.fasterxml.jackson.annotation.JsonIgnore;
import org.hibernate.validator.constraints.NotBlank;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;

/**
 * A SysUser.
 */
@Entity
@Table(name = "sys_user")
@EntityListeners(AuditingEntityListener.class)
public class SysUser extends BaseEntity {

    @Column(name = "user_name")
    @NotBlank(message = "用户名不能为空")
    private String userName;

    @Column(name = "user_password")
    @JsonIgnore
    @NotBlank(message = "密码不能为空")
    private String userPassword;

    @Column(name = "user_salt")
    @JsonIgnore
    private String userSalt;

    @Column(name = "user_available")
    private Integer userAvailable;

    @ManyToMany(targetEntity = SysRole.class,fetch = FetchType.LAZY,cascade = {CascadeType.PERSIST, CascadeType.MERGE})
    @JoinTable(name = "sys_user_role",
            joinColumns = {@JoinColumn(name = "user_Id")},
            inverseJoinColumns = {@JoinColumn(name = "role_Id")})
    @JsonIgnore
    private Set<SysRole> roles = new HashSet<SysRole>();
    @OneToMany(mappedBy = "sysUser",cascade = {CascadeType.MERGE,CascadeType.PERSIST})
    private Set<Template>templates=new HashSet<>();
    @ManyToOne
    @JoinColumn(name = "class_id")
    private Class aClass;
    /**
     * 密码盐.
     *
     * @return
     */
    @JsonIgnore
    public String getCredentialsSalt() {
        return this.userName + this.userSalt;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getUserPassword() {
        return userPassword;
    }

    public void setUserPassword(String userPassword) {
        this.userPassword = userPassword;
    }

    public String getUserSalt() {
        return userSalt;
    }

    public void setUserSalt(String userSalt) {
        this.userSalt = userSalt;
    }


    public Integer getUserAvailable() {
        return userAvailable;
    }

    public void setUserAvailable(Integer userAvailable) {
        this.userAvailable = userAvailable;
    }

    public Set<SysRole> getRoles() {
        return roles;
    }

    public void setRoles(Set<SysRole> roles) {
        this.roles = roles;
    }

    public Set<Template> getTemplates() {
        return templates;
    }

    public void setTemplates(Set<Template> templates) {
        this.templates = templates;
    }

    public Class getaClass() {
        return aClass;
    }

    public void setaClass(Class aClass) {
        this.aClass = aClass;
    }
}
